package org.steven.crm.app.api;

import com.steven.common.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Hashtable;

public class ModuleMapServlet extends HttpServlet {
	private static Hashtable<String, String[]> servletPathToModuleHash = new Hashtable<String, String[]>();
	static {
		String[][] modules = {
				{ "/page/aboutus.html", "page", "aboutusView" },
				{ "/page/register.html", "page", "registerView" },
		};

		for (int i = 0; i < modules.length; i++) {
			servletPathToModuleHash.put(modules[i][0], modules[i]);
		}
	}

	@Override
	protected void doPost(HttpServletRequest arg0, HttpServletResponse arg1)
			throws ServletException, IOException {
		arg0.setCharacterEncoding("UTF-8");
		String servletPath = arg0.getServletPath();
		System.out.println("servletPath " + servletPath);
		
		String queryString = arg0.getQueryString();

		if (queryString != null && queryString.indexOf("<") > 0) {
			arg1.sendRedirect("/index.html");
			return;
		}

		if (servletPathToModuleHash.get(servletPath) != null) {
			String[] ss = servletPathToModuleHash.get(servletPath);
			dispatchByModuleAndAction(ss[1], ss[2], arg0, arg1);
		} else if (servletPath.startsWith("/paymentResponse")) {
			dispatchPaymentResultModule(arg0, arg1);
		} else if (servletPath.equals("/api")) {
			String[] methodAndAction = getMethodAndAction(arg0);
			getServletContext().getRequestDispatcher(
					"/bussinessEntry?module=" + methodAndAction[0] + "&action=" + methodAndAction[1]).forward(arg0, arg1);
		} else if (servletPath.startsWith("/page")) {
			dispatcPageModule(arg0, arg1);
		} else if (servletPath.startsWith("/help")) {
			dispatchHelpPageModule(arg0, arg1);
		} else {
			arg1.sendRedirect("/index.html");
		}
	}

	private String[] getMethodAndAction(HttpServletRequest request) {
		String method = request.getParameter("method");
		String module = "";
		String action = "";
		if (!StringUtils.isEmpty(method)) {
			String[] apiMethod = StringUtil.split(method, ".");
			module = apiMethod[0];
			action = apiMethod[1];
		} else {
			String contentPath = request.getContextPath();
			String servletPath = request.getServletPath();
			String uri = request.getRequestURI().replace(contentPath + servletPath, "");
			String[] apiMethod = StringUtil.split(uri, "/");
			module = apiMethod[0];
			action = apiMethod[1];
		}

		return new String[]{module, action};
	}

	@Override
	protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1)
			throws ServletException, IOException {
		doPost(arg0, arg1);
	}

	public void dispatchByModuleAndAction(String module, String action,
			HttpServletRequest arg0, HttpServletResponse arg1)
			throws ServletException, IOException {
		getServletContext().getRequestDispatcher(
				"/bussinessEntry?module=" + module + "&action=" + action)
				.forward(arg0, arg1);
	}
	
	public void dispatchByParseSubID(HttpServletRequest arg0,
			HttpServletResponse arg1, String link) throws ServletException,
			IOException {
		String servletPath = arg0.getRequestURI();
		String path = servletPath.substring(servletPath.lastIndexOf("/"));
		String id = "";
		id = path.substring(1, path.length() - 5);
		int index = id.indexOf("-");
		id = id.substring(index + 1, id.length());
		arg0.setCharacterEncoding("utf-8");
		getServletContext().getRequestDispatcher(link + id).forward(arg0, arg1);
	}
	
	public void dispatchPayModule(HttpServletRequest arg0,
			HttpServletResponse arg1) throws ServletException, IOException {
		String uri = arg0.getRequestURI();
		if (uri.matches("/pay/m-[0-9]+?.html")) {
			dispatchByParseSubID(arg0, arg1,
				"/bussinessEntry?module=pay&action=mobilePayView&paymentID=");
		} else if (uri.matches("/pay/[0-9]+?.html")) {
			dispatchByParseSubID(arg0, arg1,
				"/bussinessEntry?module=pay&action=defaultView&paymentID=");
		} 
	}
	
	private void dispatchPaymentResultModule(HttpServletRequest arg0, 
			HttpServletResponse arg1) throws ServletException, IOException {
		String uri = arg0.getRequestURI();
 		String module = "payResult";
		if (uri.equals(arg0.getContextPath() + "/paymentResponse/wxPayResult")) { // 微信小程序支付回调
			dispatchByModuleAndAction(module, "wxPayResult", arg0, arg1);
		}
	}

	public void dispatchHelpPageModule(HttpServletRequest arg0,
									   HttpServletResponse arg1) throws ServletException, IOException {
		String uri = arg0.getRequestURI();
		if (uri.matches("/help/[0-9]+?.html")) {
			dispatchByParseSubID(arg0, arg1,
					"/bussinessEntry?module=page&action=gwbHelpPageInfoView&helpPageID=");
		}
	}

	public void dispatcPageModule(HttpServletRequest arg0,
								  HttpServletResponse arg1) throws ServletException, IOException {
		String uri = arg0.getRequestURI().replace(arg0.getContextPath(), "");
		if (uri.matches("/page/homeServiceProtocol/[0-9]+?.html")) {
			dispatchByParseSubID(arg0, arg1,
					"/bussinessEntry?module=page&action=homeServiceProtocolView&homeServiceId=");
		}
	}
	
	public void dispatchRecacheModule(HttpServletRequest arg0,
			HttpServletResponse arg1) throws ServletException, IOException {
		dispatchByModuleAndAction("recache", "reloadLocalCache", arg0, arg1);
	}
}
